package by.grsu.transport.converter;

import by.grsu.transport.model.User;
import by.grsu.transport.persistence.entity.UserEntity;

public class UserConverter implements ObjectConverter<User, UserEntity> {

	private UserConverter() {
	}

	private static UserConverter instance;

	public static UserConverter getInstance() {
		if (instance == null) {
			instance = new UserConverter();
		}
		return instance;
	}

	@Override
	public User entityToModel(UserEntity entity) {
		User user = new User();
		user.setId(entity.getId());
		user.setName(entity.getFirstName() + " " + entity.getLastName());
		user.setLogin(entity.getLogin());
		user.setPassword(entity.getPassword());
		user.setRegistrationDate(entity.getRegistrationDate());
	    user.setRole(User.Role.valueOf(entity.getRole()));
		return user;
	}

	@Override
	public UserEntity modelToEntity(User model) {
		UserEntity entity = new UserEntity();
		String[] userName = model.getName().split(" ");
		entity.setId(model.getId());
		entity.setFirstName(userName[0]);
		entity.setLastName(userName[1]);
		entity.setIsLocked(model.getIsLocked());
		entity.setLogin(model.getLogin());
		entity.setPassword(model.getPassword());
		entity.setRole(model.getRole().ordinal());
		entity.setRegistrationDate(model.getRegistrationDate());
		return entity;
	}

}
